!*------------------------------------------------------------
!*
!*  Project:        Clarion Open Source Project
!*                  http://clarion.googlecode.com/
!*
!*  Module:         Class cosOptions
!*  Created:        10/04/2008 11:18PM
!*
!*  The MIT License
!*
!*  Copyright (c) 2008 Mark W. Geisinger
!*
!*  Permission is hereby granted, free of charge, to any person obtaining a copy
!*  of this software and associated documentation files (the "Software"), to deal
!*  in the Software without restriction, including without limitation the rights
!*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
!*  copies of the Software, and to permit persons to whom the Software is
!*  furnished to do so, subject to the following conditions:
!*
!*  The above copyright notice and this permission notice shall be included in
!*  all copies or substantial portions of the Software.
!*
!*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
!*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
!*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
!*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
!*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
!*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
!*  THE SOFTWARE.
!*
!*  Comments:
!*
!*------------------------------------------------------------
!*
!*  History:    10/05/2008  v1.0.0.0 release
!*
!*              10/7/2008   v1.0.0.1 release to tag template change
!*
!*------------------------------------------------------------


    member()

    include('equates.clw')
    include('cos_Options.inc'), once

    map
        module('win')
            cosOutputDebugString( *cstring ), pascal, raw, name('OutputDebugStringA')
        end
    end

!
! constants
!

COS_ERROR           equate(-1)
COS_OK              equate(0)

! Overload bitwise operators
cosFlagEqual        equate(bAnd)
cosFlagSet          equate(bOr)
cosFlagClear        equate(bXor)

!
! data
!

cosOptions.Init procedure( *group groupIn, long Debug = 0 )

    code

    self.FieldCount = 1
    self.DebugOn = Debug

    self.rgOpts         &= groupIn

    loop while( Who( self.rgOpts, self.FieldCount ) <> '' )

        self.FieldCount += 1

    end

    if ( self.DebugOn )

        self.DebugOut( 'in .Init self.FieldCount = ' & self.FieldCount )

        if ( not self.FieldCount )
            self.DebugOut( 'empty group passed to .Init' )
        end

    end

cosOptions.Init procedure( *group groupIn, *cstring szFileNameIn, long Debug = 0 )

    code

    self.FieldCount = 1
    self.DebugOn = Debug

    self.rgOpts         &= groupIn
    self.szFileName     &= szFileNameIn

    loop while( Who( self.rgOpts, self.FieldCount ) <> '' )

        self.FieldCount += 1

    end

    if ( self.DebugOn )

        self.DebugOut( 'in .Init self.FieldCount = ' & self.FieldCount )

        if ( not self.FieldCount )
            self.DebugOut( 'empty group passed to .Init' )
        end

        if ( not szFileNameIn )
            self.DebugOut( 'empty file name passed to .Init' )
        end
    end

! cosOptions.Kill    procedure

!     code

    ! nothing to do here

cosOptions.IsFlag  procedure( long FieldIndex, long Mask )

FlagResult      long
FlagField       any

    code

    FlagField &= What( self.rgOpts, FieldIndex )

    if ( self.FlagCheckSanity( FieldIndex, Mask, FlagField ) )
        return( COS_ERROR )
    end

    FlagResult = cosFlagEqual( FlagField, Mask )
    FlagField &= Null
    return( FlagResult )

cosOptions.SetFlag  procedure( long FieldIndex, long Mask )

FlagField       any

    code

    FlagField &= What( self.rgOpts, FieldIndex )

    if ( self.FlagCheckSanity( FieldIndex, Mask, FlagField ) )
        return( COS_ERROR )
    end

    if ( not cosFlagEqual( FlagField, Mask ) )
        FlagField = cosFlagSet( FlagField, Mask )
        FlagField &= Null
        return( COS_OK )
    end

cosOptions.ClearFlag   procedure( long FieldIndex, long Mask )

FlagField       any

    code

    FlagField &= What( self.rgOpts, FieldIndex )

    if ( self.FlagCheckSanity( FieldIndex, Mask, FlagField ) )
        return( COS_ERROR )
    end

    if ( cosFlagEqual( FlagField, Mask ) )
        FlagField = cosFlagClear( FlagField, Mask )
        FlagField &= Null
        return( COS_OK )
    end

cosOptions.ValueSet procedure( long FieldIndex, ? ValueIn )

gField      any

    code

    if ( self.FieldCheckSanity( FieldIndex ) )
        return( COS_ERROR )
    end

    gField &= What( self.rgOpts, FieldIndex )

    gField = ValueIn
    gField &= Null

    return( 0 )

cosOptions.ValueGet procedure( long FieldIndex, *? ValueOut )

gField      any

    code

    if ( self.FieldCheckSanity( FieldIndex ) )
        return( COS_ERROR )
    end

    gField &= What( self.rgOpts, FieldIndex )

    ValueOut = gField
    gField &= Null

    return( 0 )

cosOptions.DebugOut procedure( string DebugMsg )

szDebugMsg      cstring(256)

    code

    szDebugMsg = 'cosOptions Class: ' & DebugMsg
    cosOutputDebugString( szDebugMsg )

cosOptions.FieldCheckSanity    procedure( long FieldIndex )

    code

    if ( not inRange( FieldIndex, 1, self.FieldCount ) )
        if ( self.DebugOn )
            self.DebugOut( 'in .FieldCheckSanity - invalid field index passed' )
        end
        return( COS_ERROR )
    end

    return( COS_OK )

! FLAGCHECKSANITY@Fllu

cosOptions.FlagCheckSanity  procedure( long FieldIndex, long Mask, ? FlagField )

Sanity          long( COS_OK )

    code

    if ( not inRange( FieldIndex, 1, self.FieldCount ) )
        if ( self.DebugOn )
            self.DebugOut( 'in .FlagCheckSanity - invalid field index passed' )
        end
        return( COS_ERROR )
    end

    if ( not Mask )
        if ( self.DebugOn )
            self.DebugOut( 'in .FlagCheckSanity - 0 mask passed' )
        end
        return( COS_ERROR )
    end

    if ( IsString( FlagField ) )
        if ( self.DebugOn )
            self.DebugOut( 'in .FlagCheckSanity - group field is string type. Must be long data type to operate as flags' )
        end
        Sanity = COS_ERROR
    end

    return( Sanity )

